/**
 * Created by Toma on 2016-7-4 10:31:07
 * 干货挑战~ @ver 1.0.1
 */
var cc = window.cc || {};
cc.program = new function () {
  var self = this;

  (function () {
    var docEl = document.documentElement,
        resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
        recalc = function () {
          if (docEl.clientWidth / docEl.clientHeight < 0.635) { // 0.6158
            docEl.style.fontSize = 20 * (docEl.clientWidth / 375) + 'px';
          } else {
            docEl.style.fontSize = 20 * (docEl.clientHeight / 590.6) + 'px';
          }
          self._reset();
        };
    window.addEventListener(resizeEvt, recalc, false);
    document.addEventListener('DOMContentLoaded', recalc, false);
  })();

  /* app自适应 */
  self._reset = function () {
    var w = document.body.scrollHeight * 0.635;
    if (w <= document.body.clientWidth) {
      $('.app').width(w).css({'margin-left': -w / 2, 'left': '50%'});
    } else {
      $('.app').width('100%').css({'margin-left': 0, 'left': '0'});
    }
  };

  self.ed = 0;
  self.images = images;
  self.loaded = function () {
    var size = self.images.length;
    ++self.ed;
    document.querySelector('.load-p').innerHTML = (self.ed / size * 100).toFixed(1) + '%';
    if (self.ed == size) {
      document.querySelector('.loading').style.display = 'none';
      Media.play();
    }
  };
  self.addImg = (function () {
    if (self.images) {
      for (var i = 0; i < self.images.length; i++) {
        var image = new Image();
        image.src = self.images[i];
        image.addEventListener('load', function () {
          self.loaded();
        });
      }
    }
  })();

  /* 加载音效 */
  var Media = new Audio('audio/bg.mp3');
  Media.addEventListener('loadeddata', function () {
    Media.loop = true;
  });


  /* 启动页面 */
  self.openPage = function () {
    var rule = $('.page-rule');
    $('#rule').tap(function () {
      rule.show();
      self.hash('page-rule')
    });
    var content = $('.page-content');
    $('#start').tap(function () {
      content.show();
      self.hash('page-content');
      self.swiper();
    })
  };

  /* remove start */
  var random = ['1.88', '6.88', '16.8', '188', '688', '888'],
      ran = Math.floor(Math.random() * 7),
      gate = random[ran];
  /* remove end */

  /* 报告页 */
  self.report = function () {
    /* 摇奖 */
    $('.action').on('tap', function () {
      var _this = this;
      $.ajax({
        url: '',
        type: 'get',
        success: function (data) {
          self.rotate(gate);　　// 奖金
          $(_this).off('tap');
        },
        erro: function () {
          alert('链接超时');
        }
      });
    });
  };

  self.info = {
    '1.88': 221,
    '6.88': 132,
    '16.8': 88,
    '188': 268,
    '688': 177,
    '888': 360
  };
  self.rotate = function (deg) {
    var late = 720 + self.info[deg];
    $('.pointer').css({
      'webkitTransform': 'rotate(' + late + 'deg)',
      'transform': 'rotate(' + late + 'deg)',
      'transition': 'all 2.4s cubic-bezier(0.20 , 0.65 , 0.01 , 0.95)'
    });
    setTimeout(function () {
      $('#money').html('￥' + deg);
      $('.winning').show();
    }, 3000);
  };


  /* 记录hash值 */
  self.hash = function (hash) {
    location.hash = hash;
    $(window).on('hashchange', function () {
      if (!location.hash) {
        $('.' + hash).hide();
      }
    });
  };

  /* swiper */
  self._swiper = '';
  self.swiper = function () {
    self._swiper = self._swiper || new Swiper('.swiper-container', {
          direction: 'vertical',
          onInit: function (swiper) {
            swiperAnimateCache(swiper);
            swiperAnimate(swiper);
          },
          onSlideChangeEnd: function (swiper) {
            swiperAnimate(swiper);
          }
        });
  };


  /**
   * main
   */
  window.onload = function () {
    self.openPage();
  }
};